//---------------------------------------------------------------------
// Authors: jachymko
//
// Description: Class implementing Get-Junction command.
//
// Creation Date: Dec 15, 2006
//
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Management.Automation;

using Microsoft.Win32.SafeHandles;

namespace Pscx.Commands.FileSystem.Ntfs
{
    [Cmdlet(VerbsCommon.Get, PscxNouns.ReparsePoint, DefaultParameterSetName = "Path")]
    [Description("Gets NTFS reparse point data.")]
    public class GetReparsePointCommand : PSCmdlet
    {
        string[] paths;
        string[] literalPaths;

        SwitchParameter raw;

        [Parameter(Position = 0,
                   Mandatory = true, 
                   ValueFromPipeline = true, 
                   ParameterSetName = "Path",
                   HelpMessage = "Specifies path to a reparse point. Wildcards are permitted.")]
        [AcceptsWildcards(true)]
        public string[] Path
        {
            get { return paths; }
            set { paths = value; }
        }

        [Parameter(Position = 0,
                   Mandatory = true, 
                   ValueFromPipeline = true, 
                   ParameterSetName = "LiteralPath",
                   HelpMessage = "Specifies path to a reparse point. Unlike Path, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards.")]
        [AcceptsWildcards(true)]
        public string[] LiteralPath
        {
            get { return literalPaths; }
            set { literalPaths = value; }
        }

        [Parameter(HelpMessage = "When specified, gets the raw reparse point data as a byte array.")]
        public SwitchParameter Raw
        {
            get { return raw; }
            set { raw = value; }
        }

        protected override void ProcessRecord()
        {
            foreach(string path in GetPaths())
            {
                if(ReparsePointHelper.IsReparsePoint(path))
                {
                    if(Raw.IsPresent)
                        WriteObject(ReparsePointHelper.GetReparsePointData(path));
                    else
                        WriteObject(ReparsePointHelper.GetReparsePoint(path));
                }
            }
        }

        IEnumerable<string> GetPaths()
        {
            if(ParameterSetName == "LiteralPath")
            {
                return Utils.ResolveFilePaths(this, literalPaths, true);
            }

            return Utils.ResolveFilePaths(this, paths, false);
        }
    }
}
